package com.wing.cms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.cms.domain.Portlet;
import com.wing.cms.domain.Portlet1;
import com.wing.cms.domain.Portlet2;
import com.wing.cms.domain.Portlet3;
import com.wing.cms.domain.Portlet4;
import com.wing.cms.domain.Portlet_C;
import com.wing.cms.ext.domain.CmsFunction;
import com.wing.cms.ext.domain.CustomPortletInerface;
import com.wing.cms.ext.service.CmsFunctionManager;
import com.wing.cms.service.Portlet1Manager;
import com.wing.cms.service.Portlet2Manager;
import com.wing.cms.service.Portlet3Manager;
import com.wing.cms.service.Portlet4Manager;
import com.wing.cms.service.PortletManager;
import com.wing.cms.service.Portlet_CManager;
import com.wing.cms.service.TemplateManager;
import com.wing.common.Constants;

/**
 * 
 * 
 * @author wyf
 * 
 */
 
@Controller
@RequestMapping(value = "/cms/portlet")
public class PortletController {

	@Autowired
	private PortletManager portletManager;
	@Autowired
	private Portlet1Manager portlet1Manager;
	@Autowired
	private Portlet2Manager portlet2Manager;
	@Autowired
	private Portlet3Manager portlet3Manager;
	@Autowired
	private Portlet4Manager portlet4Manager;
	@Autowired
	private TemplateManager templateManager; 
	@Autowired
	private CmsFunctionManager cmsFunctionManager;
	@Autowired
	private Portlet_CManager portlet_CManager;
	
	

	
	/**
	 * 删除portlet实例
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public @ResponseBody String delete(@PathVariable String id,String portletype) {
		if(portletype.equals("article")){
			portletManager.deletePortlet(id);
		}else if(portletype.equals("menu")){
			portlet1Manager.deletePortlet(id);
		}else if(portletype.equals("product")){
			portlet2Manager.deletePortlet(id);
		}else if(portletype.equals("content")){
			portlet3Manager.deletePortlet(id);
		}else if(portletype.equals("pictrue")){
			portlet4Manager.deletePortlet(id);
		}else{
			portlet_CManager.deletePortlet(id);
		}
		
		
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
		
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toForm(@PathVariable String id,String portletype, Model model,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		if(portletype==null)return null;
		if(portletype.equals("article")){
			Portlet portlet = null;
			if(!id.equals("0")){
				portlet = portletManager.findById(id);
			}else{
				portlet = new Portlet();
				portlet.setTitlecolor("#123456");
				portlet.setPortletheight(300);
				portlet.setPortletwidth(300);
				portlet.setArticlecount(5);
				portlet.setTitlelength(20);
				portlet.setSummarylength(200);
				
			}
			model.addAttribute("templateList",templateManager.findTemplateByUserId("article",siteId));
			model.addAttribute("portlet", portlet);
			return "/cms/portlet/portletForm";
		}else if(portletype.equals("menu")){
			Portlet1 portlet1 = null;
			if(!id.equals("0")){
				 portlet1 = portlet1Manager.findById(id);
			}else{
				portlet1 = new Portlet1();
				portlet1.setTitlecolor("#123456");
				portlet1.setPortletheight(300);
				portlet1.setPortletwidth(300);
			}
			model.addAttribute("templateList",templateManager.findTemplateByUserId("menu",siteId));
			model.addAttribute("portlet1", portlet1);
			return "/cms/portlet/portlet1Form";
		}else if(portletype.equals("product")){
			Portlet2 portlet2 = null;
			if(!id.equals("0")){
				portlet2 = portlet2Manager.findById(id);
			}else{
				portlet2 = new Portlet2();
				portlet2.setTitlecolor("#123456");
				portlet2.setPortletheight(300);
				portlet2.setPortletwidth(300);
				portlet2.setProductcount(3);
				portlet2.setTitlelength(20);
				portlet2.setSummarylength(200);
			}
			model.addAttribute("templateList",templateManager.findTemplateByUserId("product",siteId));
			model.addAttribute("portlet2", portlet2);
			return "/cms/portlet/portlet2Form";
		}else if(portletype.equals("content")){
			Portlet3 portlet3 = null;
			if(!id.equals("0")){
				portlet3 = portlet3Manager.findById(id);
			}else{
				portlet3 = new Portlet3();
				portlet3.setTitlecolor("#123456");
				portlet3.setPortletheight(300);
				portlet3.setPortletwidth(300);
				portlet3.setContentlength(500);
			}
			model.addAttribute("templateList",templateManager.findTemplateByUserId("content",siteId));
			model.addAttribute("portlet3", portlet3);
			return "/cms/portlet/portlet3Form";
		}else if(portletype.equals("pictrue")){
			Portlet4 portlet4 = null;
			if(!id.equals("0")){
				portlet4 = portlet4Manager.findById(id);
			}else{
				portlet4 = new Portlet4();
				portlet4.setTitlecolor("#123456");
				portlet4.setPortletheight(300);
				portlet4.setPortletwidth(300);
				portlet4.setPicheight(300);
				portlet4.setPicwidth(300);
			}
			model.addAttribute("templateList",templateManager.findTemplateByUserId("pictrue",siteId));
			model.addAttribute("portlet4", portlet4);
			return "/cms/portlet/portlet4Form";
		}else{
			Map cf=cmsFunctionManager.findByFun_code(portletype);
			if(cf==null){
				return null;
			}
			Portlet_C p = null;
			if(!id.equals("0")){
				p = portlet_CManager.findById(id);
			}else{
				p = new Portlet_C();
				p.setTitlecolor("#123456");
				p.setPortletheight(300);
				p.setPortletwidth(300);
			}
			model.addAttribute("templateList",templateManager.findTemplateByUserId(portletype,siteId));
			model.addAttribute("portlet_c", p);
			model.addAttribute("portletype", portletype);
			return (String)cf.get("editview");
		}
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toConfigHorder", method = RequestMethod.GET)
	public String toConfigHorder(HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		List list = cmsFunctionManager.getFunctionsBySiteId(siteId);
		request.setAttribute("funs", list);
		return "/cms/portlet/portletMain";
	}
	
	
	@RequestMapping(value = "saveportlet", method = RequestMethod.POST)
	public  @ResponseBody  String saveportlet(Portlet portlet,String horderChannelId,String placeholderId,String portletype,HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		portletManager.savePortletInfo(portlet,horderChannelId,placeholderId,portletype,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	@RequestMapping(value = "saveportlet1", method = RequestMethod.POST)
	public  @ResponseBody  String saveportlet1(Portlet1 portlet1,String horderChannelId,String placeholderId,String portletype,HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		portlet1Manager.savePortletInfo(portlet1,horderChannelId,placeholderId,portletype,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	@RequestMapping(value = "saveportlet2", method = RequestMethod.POST)
	public  @ResponseBody  String saveportlet2(Portlet2 portlet2,String horderChannelId,String placeholderId,String portletype,HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		portlet2Manager.savePortletInfo(portlet2,horderChannelId,placeholderId,portletype,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	@RequestMapping(value = "saveportlet3", method = RequestMethod.POST)
	public  @ResponseBody  String saveportlet3(Portlet3 portlet3,String horderChannelId,String placeholderId,String portletype,HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		portlet3Manager.savePortletInfo(portlet3,horderChannelId,placeholderId,portletype,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	@RequestMapping(value = "saveportlet4", method = RequestMethod.POST)
	public  @ResponseBody  String saveportlet4(Portlet4 portlet4,String horderChannelId,String placeholderId,String portletype,HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		portlet4Manager.savePortletInfo(portlet4,horderChannelId,placeholderId,portletype,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	@RequestMapping(value = "saveportlet_c", method = RequestMethod.POST)
	public  @ResponseBody  String saveportlet_c(Portlet_C portlet_c,String horderChannelId,String placeholderId,String portletype,HttpServletRequest request) throws Exception {
		Map cf=cmsFunctionManager.findByFun_code(portletype);
		if(cf==null){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		Class cls = Class.forName((String)cf.get("classz")) ;
		CustomPortletInerface cp = (CustomPortletInerface)cls.newInstance() ;
		cp.init(request);
		portlet_c.setObjectView(cp);
		String siteId = (String) request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		portlet_CManager.savePortletInfo(portlet_c,horderChannelId,placeholderId,portletype,siteId);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
}

